<!-- 机房预约情况统计 -->
<template>
  <div class="ky-container">
    <el-card shadow="hover" class="ky-card ky-card-xs ky-card-hdbg">
      <el-form
        ref="ruleForm"
        :model="formData"
        :inline="true"
        class="ky-form-inline ky-form-inline-sm"
        :rules="rules"
        label-width="84px"
      >
        <el-form-item label="医院名称:" prop="hospitalCode">
          <el-select
            size="small"
            class="param-type_input"
            v-model="formData.hospitalCode"
            placeholder="请选择"
            filterable
            @change="handleChange('hospital')"
          >
            <el-option
              v-for="(item,index) in mx_hospitalList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="影像区域:">
          <el-select
            size="small"
            class="param-type_input"
            v-model="formData.depArea"
            multiple
            placeholder="请选择"
            clearable
            @change="handleChange('depArea')"
          >
            <el-option
              v-for="(item,index) in depAreaOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查部门:">
          <el-select
            size="small"
            class="param-type_input"
            v-model="formData.dep"
            multiple
            placeholder="请选择"
            clearable
            @change="handleChange('dep')"
          >
            <el-option
              v-for="(item,index) in scheduledDepOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查设备:">
          <el-select
            size="small"
            class="param-type_input"
            v-model="formData.modality"
            multiple
            placeholder="请选择"
            clearable
            @change="handleChange('modality')"
          >
            <el-option
              v-for="(item,index) in modalityOptions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="患者类型" prop="patType">
          <el-select
            style="width:100%"
            class="param-type_input"
            size="small"
            multiple
            clearable
            v-model="formData.patType"
            placeholder="请选择"
          >
            <el-option
              v-for="item in patTypeOptions"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="100px" prop="pickerDate" class="ky-form_item_label">
          <div slot="label">
            <el-select size="small" class="label_sel" v-model="formData.dateType">
              <el-option
                v-for="(item,index) in dateTypeOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <el-date-picker
            size="small"
            v-model="formData.timeRange"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <!-- <el-button class="m-l-0 click-button" size="small" @click="resetParam" plain>重置</el-button> -->
          <el-button
            type="primary"
            class="m-l-0 click-button"
            size="small"
            @click="search"
            icon="el-icon-search"
            :loading="dataLoading"
          >查询</el-button>
          <el-button
            :loading="dataLoading||downloadLoading"
            size="small"
            type="danger"
            icon="el-icon-download"
            @click="handleDownload"
          >导出</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="hover" class="ky-card ky-mt10">
      <div slot="header" class="clearfix">
        <span>预约情况列表</span>
        <el-radio-group
          size="small"
          style="float: right; padding: 3px 0"
          v-model="formData.tabType"
          @change="changeSearch"
        >
          <el-radio-button
            v-for="(item,index) in tabTypeOptions"
            :key="index"
            :label="item.value"
          >{{item.label}}</el-radio-button>
        </el-radio-group>
      </div>

      <el-table
        ref="table"
        v-loading="dataLoading"
        size="medium"
        :data="tableData"
        border
        show-summary
        :max-height="computedHeight"
        :header-cell-style="{background:'#00c0ef', color:'#fff',fontWeight:'bold'}"
      >
        <el-table-column align="center" prop="item" :label="formData.typeLabel" min-width="120"></el-table-column>
        <el-table-column align="center" prop="zs" label="总数"></el-table-column>
        <el-table-column align="center" prop="wjc" label="未检查"></el-table-column>
        <el-table-column align="center" prop="wbg" label="未报告"></el-table-column>
        <el-table-column align="center" prop="wsh" label="未审核"></el-table-column>
        <el-table-column align="center" prop="ysh" label="已审核"></el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { Options } from '@/utils/constant'
import { dateFormatter } from '@/utils/validate'
import { getCheckRoomAppointmentStatistics, getDepAreaList, getDepList, getModalityList, getCheckRoomList } from '@/api/api.js'
export default {
  name: 'AppointmentInfo',
  mixins: [],
  data() {
    return {
      rules: {
        timeRange: { type: 'array', required: true, message: '请选择记录时间', trigger: 'change' }
      },
      formData: {
        hospitalCode: this.$store.state.user.hospitalCode,
        dep: [],
        dateType: 'ReportDate',
        timeRange: [dateFormatter(new Date(new Date() - 3600 * 1000 * 24 * 6), false), dateFormatter(new Date(), false)],
        depArea: [],
        modality: [],
        // checkRoom: [],
        patType: [],
        typeLabel: '部门',
        tabType: 1 // 1:部门,2:设备,3:机房,4:时间段
      },
      tabTypeOptions: [
        { value: 1, label: '部门' },
        { value: 2, label: '设备' },
        { value: 3, label: '机房' },
        { value: 4, label: '时间段' }
      ],
      dateTypeOptions: [
        { value: 'RegisterDate', label: '登记时间' },
        { value: 'AppointDate', label: '预约时间' },
        { value: 'CheckDate', label: '检查时间' },
        { value: 'ReportDate', label: '报告时间' },
        { value: 'AuditDate', label: '审核时间' }
      ],
      depAreaOptions: [],
      scheduledDepOptions: [],
      modalityOptions: [],
      // checkRoomOptions: [],
      patTypeOptions: Options.patientTypeItemOptions,

      dataLoading: false,
      downloadLoading: false,
      tableData: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  watch: {},
  computed: {
    computedHeight() {
      let height = window.innerHeight - 280
      return height
    }
  },
  created() {
  },
  async mounted() {
    this.$getHospitalList()
    this.formData.hospitalCode = this.$store.state.user.hospitalCode
    try {
      await this.getDepAreaOptions()
      this.getScheduledDepOptions()
      this.getModalityOptions()
      // this.getCheckRoomOptions()
    } catch (err) {
      console.log(err)
    }
    this.search()
  },
  methods: {
    resetParam() {
      // 重置
      this.formData = {
        hospitalCode: this.$store.state.user.hospitalCode,
        dep: [],
        timeRange: [dateFormatter(new Date(new Date() - 3600 * 1000 * 24 * 6), false), dateFormatter(new Date(), false)],
        depArea: [],
        modality: [],
        // checkRoom: []
        patType: [],
        typeLabel: '部门',
        tabType: 1 // 1:部门,2:设备,3:机房,4:时间段
      }
      //   this.search()
    },
    changeSearch() {
      let arr = this.tabTypeOptions.filter(item => item.value == this.formData.tabType)
      this.formData.typeLabel = arr[0].label
      this.search()
    },
    search() {
      // 查询
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.fetchData()
        }
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = [this.formData.typeLabel, '总数', '未检查', '未报告', '未审核', '已审核']
        const filterVal = ['item', 'zs', 'wjc', 'wbg', 'wsh', 'ysh']
        const list = this.tableData
        this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data: this.excelData,
          filename: this.$route.name,
          autoWidth: true
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      this.excelData = []
      jsonData.map((v, index) => {
        let tempArr = []
        filterVal.map(j => {
          if (v[j] || v[j] === 0) {
            if (j != 'children') {
              tempArr.push(v[j])
            } else {
              // 如果有children进行遍历
              this.formatJson(filterVal, v.children)
            }
          } else {
            tempArr.push('-')
          }
        })
        // console.log(tempArr)
        this.excelData.push(tempArr)
      })
    },
    fetchData() {
      this.tableData = []
      this.dataLoading = true
      let data = {
        hospitalCode: this.formData.hospitalCode,
        dateArr: this.formData.timeRange.join(','),
        TimeType: this.formData.dateType,
        Type: this.formData.tabType,
        depArea: this.formData.depArea.join(','),
        dep: this.formData.dep.join(','),
        modality: this.formData.modality.join(','),
        patType: this.formData.patType.join(',')
      }
      getCheckRoomAppointmentStatistics(data).then(async res => {
        this.dataLoading = false
        if (!res) return
        console.log('res.Conten', res.Content)
        this.tableData = res.Content
      }).catch(() => {
        this.dataLoading = false
      })
    },
    getDepAreaOptions() {
      /* 影像区域列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode
      }
      return new Promise(async resolve => {
        await getDepAreaList(params).then((res) => {
          if (res.code === 10000) {
            this.depAreaOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getScheduledDepOptions() {
      /* 检查部门列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        depArea: this.formData.depArea.join(',')
      }
      return new Promise(async resolve => {
        await getDepList(params).then((res) => {
          if (res.code === 10000) {
            this.scheduledDepOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getModalityOptions() {
      /* 检查设备列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        depArea: this.formData.depArea.join(','),
        dep: this.formData.dep.join(',')
      }
      return new Promise(async resolve => {
        await getModalityList(params).then((res) => {
          if (res.code === 10000) {
            this.modalityOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getCheckRoomOptions() {
      /* 检查机房列表 */
      let params = {
        hospitalCode: this.formData.hospitalCode,
        depArea: this.formData.depArea.join(','),
        dep: this.formData.dep.join(','),
        modality: this.formData.modality.join(',')
      }
      return new Promise(async resolve => {
        await getCheckRoomList(params).then((res) => {
          if (res.code === 10000) {
            this.checkRoomOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    async handleChange(val) {
      switch (val) {
        case 'hospital':
          this.depAreaOptions = []
          this.scheduledDepOptions = []
          this.modalityOptions = []
          this.checkRoomOptions = []
          this.formData.depArea = []
          this.formData.dep = []
          this.formData.modality = []
          // this.formData.checkRoom = []
          try {
            this.search()
            await this.getDepAreaOptions()
            this.getScheduledDepOptions()
            this.getModalityOptions()
            // this.getCheckRoomOptions()
          } catch (err) {
            console.log(err)
          }
          break
        case 'depArea':
          this.scheduledDepOptions = []
          this.modalityOptions = []
          this.checkRoomOptions = []
          this.formData.dep = []
          this.formData.modality = []
          // this.formData.checkRoom = []
          try {
            await this.getScheduledDepOptions()
            this.getModalityOptions()
            // this.getCheckRoomOptions()
          } catch (err) {
            console.log(err)
          }
          break
        case 'dep':
          this.modalityOptions = []
          this.checkRoomOptions = []
          this.formData.modality = []
          // this.formData.checkRoom = []
          try {
            await this.getModalityOptions()
            // this.getCheckRoomOptions()
          } catch (err) {
            console.log(err)
          }
          break
        // case 'modality':
        //   this.checkRoomOptions = []
        //   this.formData.checkRoom = []
        //   this.getCheckRoomOptions()
        //   break
        default:
          break
      }
    }
  },
  beforeDestroy() { },
  destroyed() { },
  beforeUpdate() { },
  updated() { }
}
</script>
<style lang='scss' scoped>
.ky-container {
  .param-type_input {
    /deep/ .el-input__inner {
      width: 180px;
    }
  }
}
.ky-form_item_label {
  >>> .el-form-item__label {
    padding-right: 0;
  }
}
.ky-card {
  position: relative;
  .el-radio-group {
    position: absolute;
    right: 20px;
    top: 5px;
  }
}
/deep/ .el-table__footer-wrapper tbody td,
/deep/ .el-table__header-wrapper tbody td {
  background-color: #01c0ef;
  color: #fff;
}

/deep/ .el-table__fixed-footer-wrapper tbody td {
  // border-top: 1px solid #01c0ef;
  background-color: #01c0ef;
  color: #fff;
}
</style>
